<?php

namespace app\api\controller;

use think\facade\Db;
use think\facade\Log;

/**
 * 供应商话费通知回调
 */
class MobileNotify extends \app\BaseController
{
    /**
     * 回调统一处理
     */
    public function handle($mobileSupplierId = '')
    {
        // todo redis cache
        $info = Db::name('mobile_supplier')
            ->field('id, code, app_id, app_secret, url')
            ->where('id', $mobileSupplierId)
            ->find();

        if (empty($info)) {
            exit('账号异常');
        }
        if (false === strpos($info['code'], '\\')) {
            $code = explode('-', $info['code'])[0]; // lechong，lechong-1024 都调用 \api\mobile\Lechong 这个类
            $class = '\\api\\mobile\\' . ucfirst($code);
        } else {
            $class = $info['code'];
        }
        if (!class_exists($class)) {
            exit('该供应商尚未对接');
        }
        $model = new $class($info);
        $data = input('');
        if (isTesting()){
            Log::info('到了话费回调：'.json_encode($data,JSON_UNESCAPED_UNICODE));
        }
        unset($data['mobileSupplierId']);
        $res = $model->notify($data);
        if (is_string($res)) {
            echo $res;
            exit;
        }

        if ($res === true) {
            return response()->code(200); // 慧仕成功
        }
        if ($res === false) {
            return response()->code(400); // 慧仕失败
        }
    }
}
